var email_Regexp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i ;
var phone_Regxp = /^[1][3584][0-9]{9}$/;

// JS正则表达式集合
var REGXP = {
		USER_NAME_REGXP:/^[\u4E00-\u9FA5A-Za-z_.-][\u4E00-\u9FA5A-Za-z0-9_.-]+$/, // 不能以数字开头,只是中文,英文,下划线,点,中划线
		NUM:/^[0-9]*$/ // 全部是数字
};

/**
 * 获取字符串的实际字符长度
 *
 * @returns {Number}
 */
String.prototype.realLen = function () {
    var len = 0;
    for (var i = 0; i < this.length; i++) {
        if (this.charCodeAt(i) > 255) {
            len += 2;
        } else {
            len++;
        }
    }
    return len;
};

/**
 * 获取字符串的实际汉字长度, 一个汉字 = 2个字符
 *
 * @returns {Number}
 */
String.prototype.chinsesLength = function () {
    var len = 0;
    for (var i = 0; i < this.length; i++) {
        if (this.charCodeAt(i) > 255) {
            len += 2;
        } else {
            len++;
        }
    }
    return Math.ceil(len / 2);
};

/**
 * 截取字符串长度
 * @param len 截取长度
 * @param s	超出长度的标识符
 * @returns {String}
 */
String.prototype.gbtrim = function(len, s) {
    var str = '';  
    var sp   = s || '';  
    var len2 = 0;  
    for (var i=0; i<this.length; i++) {  
        if (this.charCodeAt(i)>127 || this.charCodeAt(i)==94) {  
             len2 += 2;  
         } else {  
             len2 ++;  
         }  
     }  
    if (len2 <= len) {  
        return this;  
     }  
     len2 = 0;  
     len   = (len > sp.length) ? len-sp.length: len;  
    for (var i=0; i<this.length; i++) {  
        if (this.charCodeAt(i)>127 || this.charCodeAt(i)==94) {  
             len2 += 2;  
         } else {  
             len2 ++;  
         }  
        if (len2 > len) {  
             str += sp;  
            break;  
         }  
         str += this.charAt(i);  
     }  
    return str;  
} 
/**
 * 年 y,月 m,天 d,小时 h,分 M,秒 S
 * 
 * @param formatStr
 * @returns
 */
function formatTime(formatStr){
		var date = DzqDate.getDate();
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		var h = date.getHours();
		var M = date.getMinutes();
		var S = date.getSeconds();
		m = m < 10 ? "0"+m : m;
		d = d < 10 ? "0"+d : d;
		h = h < 10 ? "0"+h : h;
		M = M < 10 ? "0"+M : M;
		S = S < 10 ? "0"+S : S;
		return formatStr.replace("y",y).replace("m",m).replace("d",d).replace("h",h).replace("M",M).replace("S",S);
	}

/**
 * 年 y,月 m,天 d,小时 h,分 M,秒 S
 * 获取几天后的日期时间
 * @param formatStr
 * @returns
 */
function formatAfterTime(formatStr,num){
		var t = 1000 * 60 * 60 * 24 * num; 
		var tt = DzqDate.getDate().getTime();
		return formatTimes(formatStr,t+tt);
	}

/**
 * 格式化毫秒成时间
 * time 毫秒数
 * formatStr格式
 */
function formatTimes(formatStr,time){
	var date = new Date(time);
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	var d = date.getDate();
	var h = date.getHours();
	var M = date.getMinutes();
	var S = date.getSeconds();
	m = m < 10 ? "0"+m : m;
	d = d < 10 ? "0"+d : d;
	h = h < 10 ? "0"+h : h;
	M = M < 10 ? "0"+M : M;
	S = S < 10 ? "0"+S : S;
	return formatStr.replace("y",y).replace("m",m).replace("d",d).replace("h",h).replace("M",M).replace("S",S);
}

/**
 * 检查文本输入长度
 * @param obj	文本框对象（jquery对象）
 * @param name	字段名字
 * @param min	最小长度
 * @param max	最多长度
 * @returns {Boolean}
 */
function checkLength( obj, name, min, max ) {
	if ( obj.val().length > max || obj.val().length < min ) {
		Wenda.alert(name+"长度必须在"+min+"和"+max+"之间","提示消息",{
			"确定" : function(){
				$(this).dialog("close");
			}
		});
		return false;
	} else {
		return true;
	}
}
/**
 * 校验格式
 * @param obj
 * @param regexp
 * @param name	提示内容
 * @returns {Boolean}
 */
function checkRegexp( obj, regexp, name ) {
	if ( !( regexp.test( obj.val() ) ) ) {
		Wenda.alert(name,"提示消息",{
			"确定" : function(){
				$(this).dialog("close");
			}
		});
		return false;
	} else {
		return true;
	}
}


//防止ajax重复提交
var isSubmit = false;// 是否已经提交
//ajax请求完成事件
$("body").ajaxComplete(function(event,request, settings){
	isSubmit = false;
});
$("body").ajaxStart(function(event,request, settings){
	isSubmit = true;
});

var Wenda = {
	/**
	 * eg:
	 * params ={
				Ok: function() {
					//your function
					$( this ).dialog( "close" );
				},
				Cancel: function() {
					//your function
					$( this ).dialog( "close" );
				}
			}
	 * @param params
	 * @param content 显示内容
	 */
	alert : function(content,title,params){
		if(!document.getElementById("dialog-message")){
			$("body").append('<div id="dialog-message" title="message"></div>');
		}
		var $dialMsg = $("#dialog-message");
		$dialMsg.html(content);
		if(title && title != ""){
			$dialMsg.attr("title",title);
		}else{
			$dialMsg.attr("title","提示消息");
		}
		if(params == null || params == "" || params.length == 0){
			params = {
					"确定" : function(){
						$(this).dialog("close");
					}
				};
		}
		$( "#dialog:ui-dialog" ).dialog("destroy");
		$dialMsg.dialog({
			resizable: false,
			draggable: false,
			modal: true,
			buttons: params
		});
	},
	alertCallBack : function(content,callback,title,params){
		if(!document.getElementById("dialog-message")){
			$("body").append('<div id="dialog-message" title="message"></div>');
		}
		var $dialMsg = $("#dialog-message");
		$dialMsg.html(content);
		if(title && title != ""){
			$dialMsg.attr("title",title);
		}else{
			$dialMsg.attr("title","提示消息");
		}
		if(params == null || params == "" || params.length == 0){
			params = {
					"确定" : function(){
						callback();
						$(this).dialog("close");
					}
				};
		}
		$( "#dialog:ui-dialog" ).dialog("destroy");
		$dialMsg.dialog({
			resizable: false,
			draggable: false,
			modal: true,
			buttons: params
		});
	},
	post : function(url,params,callbackFun,type){
		//防止ajax重复提交
		if(isSubmit)return;
		if(!type)
			type = "json";
		if(url.indexOf("?") >= 0){
			url += "&timeflag="+(new Date().getTime());
		}else{
			url += "?timeflag="+(new Date().getTime());
		}
		$.post(url, params,callbackFun,type);
	},
	/**
	 * 字符串实际长度
	 * @param str
	 * @returns
	 */
	realLength:function(str) {
		if(str != null) return str.realLen();
		else return 0;
	},
	/**
	 * 根据长度截取字符串
	 * @param str	要操作的字符串
	 * @param len	要截取的长度
	 * @param s	超出长度的标识符“...”
	 * @returns
	 */
	substr:function(str,len,s){
		return str.gbtrim(len,s);
	},
	/**
	 * 对于相对长度的剩余长度。
	 * @param str	要操作字符串
	 * @param maxlen	长度
	 * @param lang	中文长度或英文
	 * @returns {Number}
	 */
	remainLen:function(str,maxlen,lang){
		var realLen = this.realLength(str);
		if(lang == null)lang = "en";
		if(lang == "cn"){
			maxlen += maxlen;
		}
		var remainLen = maxlen - realLen;
		if(remainLen >= 0){
			if(remainLen%2 != 0){
				remainLen += 1;
			}
		}else{
			if(remainLen%2 != 0){
				remainLen += -1;
			}
		}
		return remainLen/2;
	},
	/**
	 * config{
	 * 		html:"",//内容
	 * 		loadUrl:"",//加载内容的路径
	 * 		afterFun:"",//加载完内容后执行的js
	 * 		title:"",//窗口标题
	 * 		top:"",//top坐标
	 * 		left:"",//left坐标
	 * 		move:"",//是否可以拖动
	 * 		scroll:"",//是否跟着滚动条滚动
	 * 		width:"",//窗口宽度
	 * 		height:""//窗口高度
	 * }
	 */
	openWin:function(options){
		new DzqWin(options);
	},
	closeWin:function(){
		$("#modal-dialog").remove();
		$("div.modal-shadow").hide();
	},
	hideWin:function(){
		$("#modal-dialog").hide();
		$("div.modal-shadow").hide();
	},
	showWin:function(){
		$("#modal-dialog").show();
	},
	/**
	 * 设置文本输入框的高度自动曾高
	 * @param content 内容
	 * @param $obj textarea对象
	 */
	textareaAutoHeight:function(content,$obj){
		if($.browser.msie){
			if($obj.attr("onpropertychange")== null || $obj.attr("onpropertychange") == ""){
				$obj.attr("onpropertychange","this.style.posHeight=this.scrollHeight");
			}
			
			return ;
		}
		//if($obj.attr("wrap")== null || $obj.attr("wrap") == ""){
		//	$obj.attr("wrap","virtual");
		//}
		if(!document.getElementById("test-div")){
			$("body").append("<div id=\"test-div\" style=\"display:none;\"></div>");
		}
		var l = content.split("\n").length;
		$("#test-div").width($obj.width());
		$("#test-div").html(content);
		var heigh = $("#test-div").height()+l*(21);
		var h = $obj.height();
		if(heigh > h){
			$obj.height(heigh+8);
		}
		$obj.focus();
	},
	/**
	 * 图片缩放事件
	 */
	imgSizeChange : function(){
		var $_img = $("img[data-resize]");
		 $_img.each(function (){
			 
			 var imageObject = $(this);

		 var imgUrl = imageObject.attr("src");
         if (imgUrl.indexOf("imface/common")>-1) {
        	 return;
         	
         } else if (imgUrl.indexOf("img.dianziq.com/img/") > -1 && imgUrl.indexOf('_') == -1) {
        	 imageObject.css("display", "none");

        	 
        	 var index = imgUrl.lastIndexOf("/");
             var pre = imgUrl.substring(0, index + 1);
             var last = imgUrl.substring(index + 1, imgUrl.length);
             var newSrc = pre + '150x150f_' + last;
             imageObject.attr('src', newSrc);
             imageObject.css("display", "block");

         }
		 });

		
		$_img.bind("mouseover",function(){
			var _cursor=$(this).attr("alt");
		  	  if(_cursor==""){
		  	    $(this).css("cursor","url("+basePath+"/party/img/big.cur), auto");
		  	  }
		});
		
		$_img.unbind("click");
		$_img.bind("click",function(){
			var $this=$(this);
			  var _cursor=$this.attr("alt");
			  var src=$this.attr("src");
			  var data_src=$this.attr("data-src");
			  if(!data_src || data_src==""){
				  data_src=$this.attr("data-addr");
			  }
			  
			  if(_cursor==""){
				  $this.attr("src",data_src);
			    	$this.attr("alt","big"); 
			    	$this.css("cursor","url("+basePath+"/party/img/small.cur), auto");
			    	$this.attr("data-src",src);
			    	$this.css("maxWidth","500px");
			    	
			  }else{
			    	$this.attr("src",data_src);
			    	$this.css("cursor","url("+basePath+"/party/img/big.cur), auto");
			    	$this.attr("data-src",src);
			    	$this.attr("alt","");
			  }
		});
	},
	//删除内容前面无用的标签
	removeConBefHtmlTags:function(content,nodeName){
		return content;
	},
	removeConBefHtmlTags_old:function(content,nodeName){
		nodeName = nodeName.toUpperCase();
		var $_temp = $("<div>"+content+"</div>");
		//var i = 0;
		$_temp.children().each(function(){
			if(!chackNodeAndRemove(this)){
				var $_this = $(this);
				var hl = $_this.text();
				hl = hl.replace(/\n/gi,'');
				$_this.children("br").remove();
				
				var chi = $_this.children();
				
				if((hl && $.trim(hl).length > 0 ) || ( chi && chi.length > 0 )){
					return false;
				}else{
					if(this.nodeName && this.nodeName.toUpperCase() == "P")
						$(this).remove();
				}
			}
		});
		
		function chackNodeAndRemove(node){
			var b = node;
			if(b && (b.nodeName.toUpperCase() == nodeName)){
				$(b).remove();
				return true;
			}
			return false;
		}
		content = $_temp.html();
		return content;
	}
};


//===============================字符串差异比较算法
/**
 * 比较字符串
 * 
 * @param s1    原字符串
 * @param s2    新字符串
 * @return 返回一个数组，数组的元素由[是否是删除,在原字符串中的索引,添加或者删除的字符串]组成,索引坐标以原始字符串为准
 * 
 */
function compare(s1,s2){   
        var actions = [];//全部操作  
        var prev;//上一个操作
        var i1 = 0;
        var i2 = 0;     
        while(i2 < s2.length){   
            var status = have(i1,i2);
            if(status == 0){   
                    i1++;   
                    i2++;   
            }else{
                if(status == -1){   
                        if (prev && prev[0]==false && prev[1] == i1)
                                prev[2] += s2.charAt(i2);
                        else{
                                prev = [false,i1,s2.charAt(i2)];
                                actions.push(prev);
                        }
                        
                        i2++;
                }   
                else {   
                        if (prev && prev[0]==true && prev[1] + prev[2].length == i1)
                                prev[2] += s1.charAt(i1);
                        else{
                                prev = [true,i1,s1.charAt(i1)];
                                actions.push(prev);
                        }
                        
                        i1++;   
                }  
            }
        }
        
        if (i1 < s1.length)
                actions.push([true,i1,s1.substr(i1)]);
        
        return actions;
        
        function have(p,q){   
            var i = -1;   
            while (p < s1.length && q < s2.length){   
                i++;
                if (s1.charAt(p) == s2.charAt(q))   
                        return i;   
                else   
                        p++; 
            }   
            if (p >= s1.length)   
                    return  -1;   
            
            return i;   
        }   
}

/**
 * 将变化数组应用到当前字符串上
 * 
 * @param s string
 * @param actions array
 * @return string
 */
function apply(s,actions){
    var offest = 0;
    for (var i = 0;i < actions.length;i++){
        var action = actions[i];
        if (action[0]){
                s = s.substr(0,action[1] + offest) + s.substr(action[1] + action[2].length + offest);
                offest -= action[2].length;
        }
        else{
                s = s.substr(0,action[1] + offest) + action[2] + s.substr(action[1] + offest);
                offest += action[2].length;
        }
    }
    
    return s;
}


//===========================字符串差异高亮显示算法
function highlightDif(src1,src2){
	var arr = [];
	var actions = compare(src1,src2);
	for ( var i = 0; i < actions.length; i++) {
		var action = actions[i];
		if(action[0] == true || action[0]=='true'){
			var index = parseInt(action[1]),str = action[2];
			var prevstr;
			if(i == 0){
				prevstr = src1.substring(0,index);
				arr.push(prevstr);
			}else{
				var preIndex = actions[i-1][1];
				var prestr = actions[i-1][2];
				prevstr = src1.substring(prestr.length+preIndex-2,index);
				arr.push(prevstr);
			}
			arr.push("<del style='background:orange;'>"+str+"</del>");
		}else{
			var index = parseInt(action[1]),str = action[2];
			var prevstr;
			if(i == 0){
				prevstr = src1.substring(0,index);
				arr.push(prevstr);
			}else{
				var preIndex = actions[i-1][1];
				var prestr = actions[i-1][2];
				prevstr = src1.substring(prestr.length+preIndex,index);
				arr.push(prevstr);
			}
			//#EDFCE5
			arr.push("<ins style='background:#ADDA4D;text-decoration: none;'>"+str+"</ins>");
		}
		
		if(i == (actions.length-1)){
			var index = parseInt(action[1]);
			var afterstr;
			afterstr = src1.substring(index+action[2].length);
			arr.push(afterstr);
		}
	}
	
	return arr.join("");
}

/**
 * 电子圈
 */
var DianziQ = Wenda;

function setCookie(c_name,value,exdays)
{
	if(!getCookie(c_name)){
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : ";path=/; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
